<?php

namespace app\api\controller\Article;
use app\api\controller\Common;
use app\api\Logic\Article\ArticleLogic;
use app\api\Logic\CommonLogic;
use app\api\Logic\Interact\InteractLogic;
use think\Db;

/**
 * 文章
 */
class Article extends Common
{
    protected $noNeedLogin = ['*'];

    protected $interactLogic;
    protected $commonLogic;

    public function _initialize()
    {
        parent::_initialize();

        $this->interactLogic = new InteractLogic();
        $this->articleLogic = new ArticleLogic();
        $this->commonLogic = new CommonLogic();
    }

    /**
     * 获取文章分类
     *
     */
    public function getArticleTheme()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);
            if(empty($param['type'])){
                $this->error('未传入文章类别');
            }

            $where = [
                'a.type' => $param['type'],
                'a.shop_id' => ['in',[0,$param['shop_id']]]
            ];
            $list = $this->interactLogic->getThemeList($where);
            $this->success('查询成功',$list);
        }
        $this->error('请求错误');
    }

    /**
     * 获取文章分类详情
     *
     */
    public function getArticleThemeDetail()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);
            if(empty($param['theme_id'])){
                $this->error('未传入分类id');
            }


            $where = [
                'a.id' => $param['theme_id'],
            ];
            $list = $this->interactLogic->getThemeList($where);
            if(!$list){
                $this->error('分类不存在');
            }
            $this->success('查询成功',$list[0]);
        }
        $this->error('请求错误');
    }

    /**
     * 获取文章列表
     *
     * @return void
     */
    public function getArticleList()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);
            $theme_id = input('theme_id',0);
            $type = input('type',0);
            $order_type = input('order_type',1);

            $list = $this->articleLogic->getArticleList($param['shop_id'],$param['user_id'],$param['page'],$param['page_size'],$type,$theme_id,$order_type);
            $this->success('获取成功',$list);
        }   
        $this->error('请求错误');
    }

    /**
     * 获取文章详情
     *
     */
    public function getArticleDetail()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入文章id');
            }
            $data = $this->articleLogic->getArticleDetail($param);
            if(!$data){
                $this->error('文章不存在或未审核');
            }
            $this->success('获取成功',$data);
        }
        $this->error('请求错误');
    }

    /**
     * 萌娃圈 上下滑获取视频
     *
     * @return void
     */
    public function videoNext()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);
            $order_type = input('order_type',1);

            $list = $this->articleLogic->video_next($param,$order_type);
            $this->success('获取成功',$list);
        }   
        $this->error('请求错误');
    }



    /**
     * 文章点赞
     *
     */
    public function clickLikes()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入文章id');
            }

            $res = $this->commonLogic->giveLikes($param['user_id'],$param['shop_id'],$param['relation_id'],'Article','article','like');
            if($res){
                $this->success('操作成功',['status'=>$res]);
            }
            $this->error('操作失败');
        }
        $this->error('请求错误');
    }

    /**
     * 文章分享成功
     *
     */
    public function shareSuccess()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入文章id');
            }
            $res = $this->commonLogic->handleShare($param['user_id'],$param['shop_id'],$param['relation_id'],'Article','article','share');
            if($res){
                $this->success('操作成功');
            }
            $this->error('操作失败');
        }
        $this->error('请求错误');
    }

    /**
     * 评论提交
     *
     * @return void
     */
    public function releaseArticleComment()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);
            $to_id = input('to_id',0);
            if(empty($param['relation_id']) || empty($param['content']) ){
                $this->error('参数不完整');
            }
            $res = $this->commonLogic->insertComment($param['shop_id'],'Article',2,$param['relation_id'],$param['user_id'],$param['content'],$to_id);
            if($res){
                $this->success('发布成功');
            }
            $this->error('发布失败');
        }
        $this->error('请求错误');
    }

    /**
     * 获取评论列表
     *
     */
    public function articleCommentList()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入文章id');
            }
            $list = $this->commonLogic->getCommentList('Article',$param['relation_id'],$param['page'],$param['page_size']);
            $where = ['a.style' => 'Article','a.relation_id' => $param['relation_id'],'a.deletetime' => null];
            $count = model('CommentModel')->alias('a')->where($where)->count();
            $this->success('获取成功',['count'=>$count,'list'=>$list]);
        }
        $this->error('请求错误');
    }


}
